package com.watson.btp.controller;


import cn.hutool.core.util.XmlUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.watson.btp.ws.config.WsClient;
import com.watson.btp.ws.dto.NewQuote;
import com.watson.btp.ws.dto.NewQuoteResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author 胡广
 * @version 1.0
 * @date 2020-03-31
 * @since JDK1.8
 */
@Api(tags = "CPQ的WebService接口")
@RestController
@RequestMapping("cpq")
@Slf4j
public class CPQController {
    @Resource
    WsClient wsClient;


    @ApiOperation(value = "newQuote", notes = "newQuote")
    @PostMapping(value = "/newQuote")
    public Map<String,Object> newQuote(@RequestBody String content) throws Exception{

        NewQuote quote = new NewQuote();
        quote.setUsername("yanzhanghong#ASWATSONHLTH_TST");
        quote.setPassword("Welcome2");
        quote.setOwner("yanzhanghong");

        NewQuote.Xml xml = new NewQuote.Xml();

        Document doc = XmlUtil.parseXml(content);

        xml.getContent().add(doc.getDocumentElement());

        quote.setXml(xml);

        NewQuoteResponse newQuoteResponse = wsClient.newQuote(quote);
        NewQuoteResponse.NewQuoteResult result = newQuoteResponse.getNewQuoteResult();

        return XmlUtil.xmlToMap((Node)result.getContent().iterator().next());
    }


    @ApiOperation(value = "newQuoteJson", notes = "newQuoteJson")
    @PostMapping(value = "/newQuoteJson")
    public Map<String,Object> newQuoteJson(@RequestBody JSONObject content) throws Exception{

        NewQuote quote = new NewQuote();
        quote.setUsername("yanzhanghong#ASWATSONHLTH_TST");
        quote.setPassword("Welcome2");
        quote.setOwner("yanzhanghong");

        NewQuote.Xml xml = new NewQuote.Xml();

        String xmlStr = JSONUtil.toXmlStr(content);

        System.out.println("转化后的xml");
        System.out.println(xmlStr);

        Document doc = XmlUtil.parseXml(xmlStr);

        xml.getContent().add(doc.getDocumentElement());

        quote.setXml(xml);

        NewQuoteResponse newQuoteResponse = wsClient.newQuote(quote);
        NewQuoteResponse.NewQuoteResult result = newQuoteResponse.getNewQuoteResult();

        return XmlUtil.xmlToMap((Node)result.getContent().iterator().next());
    }

    @ApiOperation(value = "newQuoteOld", notes = "newQuoteOld")
    @PostMapping(value = "/newQuoteOld")
    public Map<String,Object> newQuoteOld(@RequestBody Map<String,Object> content) throws Exception{

        NewQuote quote = new NewQuote();
        quote.setUsername("yanzhanghong#ASWATSONHLTH_TST");
        quote.setPassword("Welcome2");
        quote.setOwner("yanzhanghong");

        NewQuote.Xml xml = new NewQuote.Xml();

        String xmlStr = new XmlMapper().writeValueAsString(content);
        Document doc = XmlUtil.parseXml(xmlStr);

        NodeList nodeList =  doc.getDocumentElement().getChildNodes();

        for(int i=0; i< nodeList.getLength(); i++) {
            xml.getContent().add(nodeList.item(i));
        }

        quote.setXml(xml);

        NewQuoteResponse newQuoteResponse = wsClient.newQuote(quote);
        NewQuoteResponse.NewQuoteResult result = newQuoteResponse.getNewQuoteResult();

        return XmlUtil.xmlToMap((Node)result.getContent().iterator().next());
    }




}
